package tgc.rj.zz.form;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import tgc.rj.zz.entity.Invoice;

public class InvoiceForm {
	private Integer id;
	private Integer page;
	private Integer rows;
	private String order;
	private String sort;
	private String number;
	private String serial;   //编号
	private String code;    //发票代码
	private float money;    //金额
	private String date;      //开票日期
	private String name;    //报销人
	private String endDate;   //报销时间
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getSerial() {
		return serial;
	}
	public void setSerial(String serial) {
		this.serial = serial;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public float getMoney() {
		return money;
	}
	public void setMoney(float money) {
		this.money = money;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public Pageable buildPageable() {
		page--;
		return PageRequest.of(page, rows);   
	}
	public Specification<Invoice> buildSpes() {
		Specification<Invoice> spec= new Specification<Invoice>() {

					@Override
					public Predicate toPredicate(Root<Invoice> root, CriteriaQuery<?> query,CriteriaBuilder criteriaBuilder) {
						HashSet<Predicate> rules=new HashSet<>();
						if(StringUtils.hasText(serial)) {
							rules.add(criteriaBuilder.like(root.get("serial"), "%"+serial+"%"));
						}else if(StringUtils.hasText(name)) {
							rules.add(criteriaBuilder.like(root.get("name"), "%"+name+"%"));
						}
						return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
					}
		};
		return spec;
	}
	
}
